import type { BaseResponse } from "@/api/response";
import { http } from "@/utils/http";

export class K8sApiService {
  apiUrl = cluster_name => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis`;
  };
  apiServerUrl = cluster_name => {
    return `${this.apiUrl(cluster_name)}/apiregistration.k8s.io/v1/apiservices`;
  };

  listApis(cluster_name): BaseResponse<any> {
    return http.get(`${this.apiUrl(cluster_name)}`);
  }

  createApiServer(cluster_name, data) {
    return http.post(`${this.apiServerUrl(cluster_name)}`, data);
  }
  updateApiServer(cluster_name, name, data) {
    return http.put(`${this.apiServerUrl(cluster_name)}/${name}`, data);
  }
  getApiService(cluster_name, name) {
    return http.get(`${this.apiServerUrl(cluster_name)}/${name}`);
  }
  listApiServices(cluster_name) {
    return http.get(`${this.apiServerUrl(cluster_name)}`);
  }

  listResourceApi(cluster_name, apiGroup, version) {
    return http.get(`${this.apiUrl(cluster_name)}/${apiGroup}/${version}`);
  }

  listPodMetrics(cluster_name, namespace, labelSelector, name) {
    const param: any = {};
    if (namespace && namespace !== "All Namespaces") {
      param.fieldSelector = "metadata.namespace=" + namespace;
    }
    if (name) {
      param.fieldSelector = "metadata.name=" + name;
    }

    if (labelSelector) {
      param.labelSelector = labelSelector;
    }

    return http.get(
      `${this.apiUrl(cluster_name)}/metrics.k8s.io/v1beta1/pods`,
      param
    );
  }

  listNodeMetrics(cluster_name) {
    return http.get(
      `${this.apiUrl(cluster_name)}/metrics.k8s.io/v1beta1/nodes`
    );
  }
}
